package com.java.business.system.permis.web;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.java.business.system.permis.bean.Role;
import com.java.business.system.permis.service.RoleService;
import com.java.core.bean.BaseController;
import com.java.core.bean.ResultBean;
import com.java.core.utils.ResponseUtils;

@RequestMapping("/system/permis/role")
@Controller
public class RoleController extends BaseController {
	@Autowired
	private RoleService service;

	@RequestMapping("/add")
	public void add(HttpServletResponse response, Role role) {
		ResultBean rb = new ResultBean();
		try {
			service.add(role);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/update")
	public void update(HttpServletResponse response, Role role) {
		ResultBean rb = new ResultBean();
		try {
			service.update(role);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, Role role) {
		ResultBean rb = new ResultBean();
		try {
			service.delete(role);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/setDisabled")
	public void setDisabled(HttpServletResponse response, Role role) {
		ResultBean rb = new ResultBean();
		try {
			service.setDisabled(role);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/setMenus")
	public void setMenus(HttpServletResponse response, String roleid,
			String[] menuids) {
		ResultBean rb = new ResultBean();
		try {
			service.setMenus(roleid, menuids);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/getRoleList")
	public void getRoleList(HttpServletResponse response, Role role) {
		ResultBean rb = new ResultBean();
		try {
			List<Role> list = service.getRoleList(role);
			rb.setData(list);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/getRoleListByUser")
	public void getRoleListByUser(HttpServletResponse response, String userid) {
		ResultBean rb = new ResultBean();
		try {
			List<Role> list = service.getRoleListByUser(userid);
			rb.setData(list);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/getRoleListNotUser")
	public void getRoleListNotUser(HttpServletResponse response, String userid) {
		ResultBean rb = new ResultBean();
		try {
			List<Role> list = service.getRoleListNotUser(userid);
			rb.setData(list);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}
}
